import {Component, OnInit, ViewContainerRef} from '@angular/core';
import {HwlmeditComponent} from "../hwlmedit/hwlmedit.component";
import {NzContextMenuService, NzDropdownMenuComponent} from "ng-zorro-antd/dropdown";
import {NzTreeVo} from "../../../../vo/NzTreeVo";
import {NzModalService} from "ng-zorro-antd/modal";
import {AdminService} from "../../../../services/admin.service";
import {AlertService} from "../../../../services/alert.service";
import {NzFormatEmitEvent} from "ng-zorro-antd/core/tree";

@Component({
  selector: 'app-hwlmlist',
  templateUrl: './hwlmlist.component.html',
  styleUrls: ['./hwlmlist.component.css']
})
export class HwlmlistComponent implements OnInit {

  tl:Array<NzTreeVo>;
  activatedNode:any;
  searchValue='';
  typeid:string;
  dl:Array<any>;
  constructor(
    private nzContextMenuService: NzContextMenuService
    ,private modal: NzModalService, private viewContainerRef: ViewContainerRef
    ,private deptservice:AdminService
    ,private alertservice:AlertService
  ) {
    this.tl=new Array<NzTreeVo>();
    this.dl=new Array<any>();
  }

  ngOnInit(): void {

    this.getReferTypeList();
  }

  getReferTypeList(){
    this.deptservice.getNztreeCargocate()
      .then((data:any)=>{
        if(data.msg=='ok'){
          this.tl=new Array<NzTreeVo>();
          this.tl.push(data.list);
         // this.tl=data.list;
        }
        else {
          this.alertservice.showError('获取参考数据异常，请重试');
        }
      })


  }


  onClickNode(data:NzFormatEmitEvent){

    this.activatedNode = data.node!;
    // this.getReferDataByTypeId();
  /*  if(this.activatedNode.parentNode==null){
      this.activatedNode.key = 0;
    }*/
  // alert(this.activatedNode.key)
  }

  contextMenu($event: MouseEvent, menu: NzDropdownMenuComponent,key:string): void {
    this.nzContextMenuService.create($event, menu);
    this.typeid=key;
  }

  addReferType(){
    const modalwindow= this.modal.create({
      nzTitle: '商品类目信息编辑',
      nzContent: HwlmeditComponent,
      nzViewContainerRef: this.viewContainerRef,
      nzComponentParams: {
        optype:'add',
        pid:this.activatedNode.key
      },

      nzOnOk:()=>new Promise(resolve => {
        // modalwindow.getContentComponent().save();

        let temp=modalwindow.getContentComponent().t;
        if(!temp.name){
          this.alertservice.showWarning('名称信息必填');
          resolve(false)
        }
        else {
          modalwindow.getContentComponent().save()

          setTimeout(()=>{

            resolve(true);
            this.getReferTypeList();
          }, 1000)
        }


      })
    })
  }


  updateReferType(){
    const modalwindow= this.modal.create({
      nzTitle: '商品类目信息编辑',
      nzContent: HwlmeditComponent,
      nzViewContainerRef: this.viewContainerRef,
      nzComponentParams: {
        optype:'update',
        pid:this.activatedNode.key
      },

      nzOnOk:()=>new Promise(resolve => {
        // modalwindow.getContentComponent().save();

        let temp=modalwindow.getContentComponent().t;
        if(!temp.name){
          this.alertservice.showWarning('名称信息必填');
          resolve(false)
        }
        else {
          modalwindow.getContentComponent().save()

          setTimeout(()=>{

            resolve(true);
            this.getReferTypeList();
          }, 1000)
        }


      })
    })

  }


  deleteReferType(){
    if(this.typeid=="root"){
      this.alertservice.showError('不能删除根节点');
    }else {
      this.deptservice.deleteCargocate(this.typeid).then((data:any)=>{
        if(data.msg=='ok'){
          this.getReferTypeList();
        }
        else {
          this.alertservice.showError('删除类型异常，请重试');
        }
      })
    }

  }

  /*  sendReferDateToCache(){
      this.deptservice.sendReferDateToCache()
        .then((data:any)=>{
          if(data.msg=='ok'){
            this.getReferTypeList();
          }
        })
    }*/

}
